package com.bytech.common.config;

import java.util.List;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.bytech.common.biz.intercept.BlogUserAgentIntercept;
import com.bytech.common.biz.intercept.BlogUserAgentMethodArgumentResolver;

/**
 * 缺省的拦截器与参数注入器配置
 * @Author zhao
 * @Create 2021/7/2815:24
 */
@Configuration
@ConditionalOnClass(WebMvcConfigurer.class)
@ConditionalOnMissingBean(name = "webMvcConfigurer")
public class SpringWebMvcConfig implements WebMvcConfigurer {

    @Value("${service.interceptor.pathPatterns}")
    private String pathPatterns;
    @Value("${service.interceptor.excludePathPatterns}")
    private String excludePathPatterns;

    @Resource
    private BlogUserAgentIntercept blogUserAgentIntercept;

    @Override
    public void addInterceptors(InterceptorRegistry interceptor) {
        String[] pathPatternArr = pathPatterns.split(";");
        String[] excludePathPatternArr = excludePathPatterns.split(";");
        interceptor.addInterceptor(blogUserAgentIntercept)
                .addPathPatterns(pathPatternArr)
                .excludePathPatterns(excludePathPatternArr);
    }

    @Resource
    private BlogUserAgentMethodArgumentResolver blogUserAgentMethodArgumentResolver;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(blogUserAgentMethodArgumentResolver);
    }
}
